package com.gccloud.design.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gccloud.design.dao.InterfaceDao;
import com.gccloud.design.entity.InterfaceEntity;
import com.gccloud.design.service.InterfaceQueryService;
import com.gccloud.starter.common.dto.SearchDTO;
import com.gccloud.starter.core.vo.PageVO;
import com.gccloud.starter.mybatis.utils.QueryWrapperUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 实现功能【接口配置业务实现层】
 *
 * @author hu.hongdou
 * @date 2022/2/21 17:31
 */
@Slf4j
@Service
public class InterfaceQueryServiceImpl extends ServiceImpl<InterfaceDao, InterfaceEntity> implements InterfaceQueryService {

    @Override
    public PageVO<InterfaceEntity> getInterfaceList(SearchDTO searchDTO) {
        LambdaQueryWrapper<InterfaceEntity> queryWrapper = QueryWrapperUtils.wrapperLike(
                        new LambdaQueryWrapper<>(), searchDTO.getSearchKey(), InterfaceEntity::getIoCd, InterfaceEntity::getIoName)
                .eq(InterfaceEntity::getStatus, 0);
        Page<InterfaceEntity> searchPage = new Page<>(searchDTO.getCurrent(), searchDTO.getSize());
        Page<InterfaceEntity> page = page(searchPage, queryWrapper);
        return new PageVO<>(page);
    }

    @Override
    public InterfaceEntity getInterfaceByCode(String code) {
        InterfaceEntity interfaceEntity = getOne(new QueryWrapper<InterfaceEntity>().eq("IO_CD", code).eq("status", 0));
        return interfaceEntity;
    }

    @Override
    public boolean saveInterfaceInfo(InterfaceEntity entity) {
        entity.setStatus(0);
        entity.setShardingId(0);
        if (entity.getScriptCode() == null) {
            entity.setScriptCode("");
        }
        if (entity.getScriptType() == null) {
            entity.setScriptType(0);
        }
        if (entity.getMockRet() == null) {
            entity.setMockRet("");
        }
        if (entity.getIsMock() == null) {
            entity.setIsMock(0);
        }
        if (entity.getInPara() == null) {
            entity.setInPara("");
        }
        try {
            return saveOrUpdate(entity);
        } catch (Exception e) {
            log.error(ExceptionUtils.getStackTrace(e));
            return false;
        }

    }


    @Override
    public boolean updateInterfaceInfo(InterfaceEntity entity) {
        if (entity.getIoId() == null || entity.getIoId() == 0) {
            throw new IllegalArgumentException("记录不存在");
        }
        UpdateWrapper<InterfaceEntity> updateWrapper = new UpdateWrapper();
        try {
            updateWrapper.set("IO_NAME", entity.getIoName())
                    .set("IO_CD", entity.getIoCd())
                    .set("IO_TYPE", entity.getIoType())
                    .set("URL", entity.getUrl())
                    .set("SERVER_CD", entity.getServerCd())
                    .set("AUTHEN_CODE", entity.getAuthenCode())
                    .set("CONSUMER_SYS_CODE", entity.getConsumerSysCode())
                    .set("SERVER_SYS_CODE", entity.getServerSysCode())
                    .set("COMMENTS", entity.getComments())
                    .eq("IO_ID", entity.getIoId());

            return update(updateWrapper);
        } catch (Exception e) {
            log.error(ExceptionUtils.getStackTrace(e));
            return false;
        }
    }

    @Override
    public boolean delInterface(Long ioId) {
        if (ioId == null || ioId == 0) {
            throw new IllegalArgumentException("记录不存在");
        }
        UpdateWrapper<InterfaceEntity> updateWrapper = new UpdateWrapper();
        updateWrapper.set("STATUS", 1).eq("IO_ID", ioId);
        try {
            return update(updateWrapper);
        } catch (Exception e) {
            log.error(ExceptionUtils.getStackTrace(e));
            return false;
        }

    }

    @Override
    public boolean saveInterfaceParam(String comments, String ioId) {
        if (StringUtils.isEmpty(ioId)) {
            throw new IllegalArgumentException("记录不存在");
        }
        UpdateWrapper<InterfaceEntity> updateWrapper = new UpdateWrapper();
        try {
            updateWrapper.set("COMMENTS", comments).eq("IO_ID", ioId);
            return update(updateWrapper);
        } catch (Exception e) {
            log.error(ExceptionUtils.getStackTrace(e));
            return false;
        }
    }


}
